#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
	bool isHappy(int n) {
		auto fun = [&](int num)
		{
			int sum = 0;
			do
			{
				sum += (num % 10)*(num % 10);
			} while (num /= 10);
			return sum;
		};
		vector<int> record;
		int t = fun(n);
		if (t == 1)
			return true;
		record.push_back(t);
		int count = 0;
		while((t=fun(t))!=1)
		{
			if (find(record.begin(), record.end(), t) != record.end())
				return false;
			record.push_back(t);
		}
		return true;		
	}
};
int main()
{
	Solution sol;
	int ret = sol.isHappy(1);
	cout << ret << endl;
	ret = sol.isHappy(19);
	cout << ret << endl;
	return 0;
}